package com.yanqu.road.server.cross3server.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.logic.activity.entername.ChannelEnterNameHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.cross3server.manager.Cross3EnterNameMgr;
import com.yanqu.road.server.protocol.Cross3Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = Cross3Protocol.C3_ENTER_NAME_ALL_NAME_SYNC, desc = "同步整个渠道报名数据")
public class Cross3EnterNameSyncFromChannelCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        EnterNameProto.ChannelEnterNameAllSyncMsg syncMsg = EnterNameProto.ChannelEnterNameAllSyncMsg.parseFrom(yanQuMessage.getBytes());
        int activityId = syncMsg.getActivityId();
        Map<Long, UserEnterName> map = new ConcurrentHashMap<>();
        for (EnterNameProto.UserEnterNameMsg msg : syncMsg.getNameList()) {
            map.put(msg.getUserId(), ChannelEnterNameHelper.parseFromMsg(msg));
        }

        List<ActivityGroupMatch> list = new ArrayList<>();
        for (EnterNameProto.ChannelEnterNameGroupingMsg groupingMsg : syncMsg.getGroupList()) {
            ActivityGroupMatch match = new ActivityGroupMatch();
            match.setActivityId(activityId);
            match.setGroupId(groupingMsg.getGroupId());
            match.setEnemyGroupId(groupingMsg.getEnemyGroupId());
            match.setZoneIdList(groupingMsg.getZoneListList());
            list.add(match);
        }
        if(list.size() > 0){
            Cross3EnterNameMgr.getMatchMap().put(activityId, list);
        }

        if(map.size() > 0){
            Cross3EnterNameMgr.add(activityId, map);
            Cross3EnterNameMgr.setDataSync(true);
        }
    }
}
